//
//  ChangePassViewController.m
//  fitnessmanage
//
//  Created by wujian on 2017/12/15.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "ChangePassViewController.h"
#import "ChangePassWordView.h"

@interface ChangePassViewController (){
    
    NSTimer *_timer;
    
    int _count;
}

@property(nonatomic,weak)ChangePassWordView *changePassWordView;

@end

@implementation ChangePassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BackGroundColor;
    
     _count = 30;
    
    [self createSubView];
    
    self.title = @"更改密码";
}

#pragma mark 创建UI
-(void)createSubView{
    
    ChangePassWordView *changePassWordView = [[ChangePassWordView alloc] init];
    
    //点击获取验证码
    [changePassWordView.sendVercodeBtn addTarget:self action:@selector(sendVerCodeBtnClick:)];
    
    [changePassWordView.sureBtn addTarget:self action:@selector(sureBtnClick)];
    
    [self.view addSubview:changePassWordView];
    
    [changePassWordView mas_remakeConstraints:^(MASConstraintMaker *make) {
    
        make.edges.mas_equalTo(self.view);
    }];
    
    self.changePassWordView = changePassWordView;
}

#pragma mark 获取验证码
-(void)sendVerCodeBtnClick:(UIButton *)sender{
    
    //先验证是不是手机号码
    NSString *mobile = self.changePassWordView.phoneTF.text;
    
    if (![mobile isMobileNumber]) {
        
        [MBProgressHUD showText:@"请输入正确的手机号码" toview:self.view];
        
        return;
    }
    
    [self startTimerWithButton:sender];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:mobile forKey:@"PhoneNumber"];
    
    [HTTPTool POSTRaw:Post_Home_SendSMS parameters:params success:^(id responseObject) {

        if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0){

            [MBProgressHUD showText:@"发送验证码成功"];
        }
        else{

            [MBProgressHUD showText:@"请输入正确的手机号"];
        }

    } failure:^(NSError *error) {

    }];
}

//启动定时器
- (void)startTimerWithButton:(UIButton *)button
{
    button.enabled = NO;
    
    if (!_timer) {
        
        _timer = [[NSTimer alloc]initWithFireDate:[NSDate date] interval:1 target:self selector:@selector(count:) userInfo:button repeats:YES];
        
        [[NSRunLoop mainRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
    }
}

//倒计时
- (void)count:(NSTimer *)timer
{
    UIButton *button = timer.userInfo;
    
    if (_count == 0) {
        
        button.enabled = YES;
        
        [_timer invalidate];
        
        _timer = nil;
        
        _count = 30;
    }
    _count --;
    [button setTitle:[NSString stringWithFormat:@"%d S",_count] forState:UIControlStateDisabled];
}


#pragma mark 确定按钮点击
-(void)sureBtnClick{
    
    NSString *mobile = self.changePassWordView.phoneTF.text;
    
    NSString *verCode = self.changePassWordView.verCodeView.titleTF.text;
    
    NSString *newPwd = self.changePassWordView.passWordView.titleTF.text;
    
    NSString *confirmPwd = self.changePassWordView.confirmPassView.titleTF.text;
    
    if ([mobile isEmptyString] || [verCode isEmptyString] || [newPwd isEmptyString] || [confirmPwd isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入完整内容" toview:self.view];
        
        return;
    }
    
    //Post_Change_Pwd
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:mobile forKey:@"PhoneNumber"];
    
    [params setObject:verCode forKey:@"Code"];
    
    [params setObject:newPwd forKey:@"Pwd"];
    
    [HTTPTool POSTRaw:Post_Change_Pwd parameters:params success:^(id responseObject) {
        
        if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0){
            
            [MBProgressHUD showText:@"修改成功"];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        else{
            
            [MBProgressHUD showText:@"请输入正确的手机号"];
        }
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)dealloc{
    
    [_timer invalidate];
    
    _timer = nil;
}


@end
